package com.example.demo.reflect;

import com.example.demo.entity.NorthPerson;
import com.example.demo.entity.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;

/**
 * @author xkx
 * @description
 * @since 2019/3/22 17:01
 */
public class XkxReflect {


  @Test
  public void reflectDemoOne()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {

    Class<? extends Person> northPersonClass = NorthPerson.class;
    Constructor<? extends Person> constructor = northPersonClass
        .getDeclaredConstructor(String.class);
    Person northPerson =  constructor.newInstance("北方");
    Field field = northPersonClass.getDeclaredField("location");
    field.setAccessible(Boolean.TRUE);
    Object value = field.get(northPerson);
    System.out.println(value);
  }
}
